﻿#if NETSTANDARD2_0 || NETCOREAPP3_0
using Microsoft.Extensions.Logging;
using System;
using System.Reflection;

namespace Longbow.Configuration
{
    /// <summary>
    /// 配置项操作类
    /// </summary>
    internal static class ConfigurationOptionsAliasUtilities
    {
        private const string AliasAttibuteAliasProperty = "Alias";

        /// <summary>
        /// 通过类型获取配置项键值方法
        /// </summary>
        /// <param name="providerType"></param>
        /// <returns></returns>
        public static string GetAlias(Type providerType)
        {
            var ret = string.Empty;
            foreach (var attribute in providerType.GetTypeInfo().GetCustomAttributes(false))
            {
                if (attribute.GetType().FullName == typeof(ProviderAliasAttribute).FullName)
                {
                    var valueProperty = attribute
                        .GetType()
                        .GetProperty(AliasAttibuteAliasProperty, BindingFlags.Public | BindingFlags.Instance);

                    ret = valueProperty.GetValue(attribute) as string;
                    break;
                }
            }
            return ret;
        }
    }
}
#endif
